//
//  MainViewController.swift
//  DSWeibo
//
//  Created by liuzhu on 2018/10/29.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        do {
            //读取plist文件内容
            let path:String = Bundle.main.path(forResource: "MainVCSettings", ofType:"plist") ?? ""
            let data:NSMutableArray = try NSMutableArray.init(contentsOfFile: path)!
                for dic in data as![[String:String]]  {
                    self.addChild(childControllerName: dic["vcName"]!, title: dic["title"]!, image: dic["imageName"]!)
                }
        }
        catch let err as NSError{
            self.addChild(childControllerName: "HomeTableViewController", title: "首页", image: "tabbar_home")
            self.addChild(childControllerName: "DiscoverTableViewController", title: "发现", image: "tabbar_discover")
            self.addChild(childControllerName: "NullViewController", title: "", image: "")
            self.addChild(childControllerName: "MessageTableViewController", title: "消息", image: "tabbar_message_center")
            self.addChild(childControllerName: "ProfileTableViewController", title: "我", image: "tabbar_profile")
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        setupComposeBtn()
    }
    
    private func setupComposeBtn(){
        tabBar.addSubview(composeBtn)
        let width = UIScreen.main.bounds.size.width/CGFloat(viewControllers!.count)
        let rect = CGRect(x: UIScreen.main.bounds.size.width/2 - width/2, y: 0, width: width, height: 49)
        composeBtn.frame = rect
    }
    
    /// 初始化子控制器-根据名称创建控制器对象
    /// - Parameters:
    ///   - childControllerName:控制器名称
    ///   - title: 标题
    ///   - image: 图片
    private func addChild(childControllerName: String,title:String,image:String) {
       guard let namespace = Bundle.main.object(forInfoDictionaryKey:"CFBundleExecutable")as? String else{
        NSLog("获取命名空间失败")
        return
        }
        let cls:AnyClass?=NSClassFromString(namespace+"."+childControllerName)
        guard let typeClass = cls as?UIViewController.Type else {
            NSLog("cls 不能当做UITableviewcontroller")
            return
        }
        let childController = typeClass.init()
        childController.tabBarItem.image = UIImage.init(named: image)
        childController.tabBarItem.selectedImage=UIImage.init(named: image+"_highlighted")
        childController.title = title

        //包装一个导航控制器
        let nav = UINavigationController()
        nav.addChild(childController)
        //将导航控制器添加到当前控制器
        self.addChild(nav)
    }
    
    @objc func composeBtnClick() {
        print(#function)
    }
    
    private lazy var composeBtn:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), for:UIControl.State.normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for:UIControl.State.highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: UIControl.State.normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: UIControl.State.highlighted)
        btn.addTarget(self, action:#selector(composeBtnClick), for: UIControl.Event.touchUpInside)
        return btn
    }()
    
}

